---
title: "Replication Extention Tables"
author: "Aidan Connaughton, Kiran Misra, Jan-Paul Sandman, and Julius Wilson"
date: "2022-11-27"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(haven)
library(fixest)
library(plyr)
library(tidyverse)
library(modelsummary)
library(kableExtra)

```

```{r echo = FALSE, message = FALSE, warning=FALSE}

# Open Replication Data
regdata <- read_dta("MainRegressions.dta")

# Select only columns that are used in regressions
regdata %>% select(approval,
                   approval_rep,
                weight_cumulative, 
                voted_for_inc, 
                votematchmean2, 
                female, 
                dem, 
                cong, 
                copartisan, 
                majority, 
                chair, 
                seniority, 
                power, 
                dist_id, 
                dist_id2, 
                icpsr, 
                gender_respondent, 
                newsint, 
                votematchmean_womens, 
                votematchmean_mens) -> data 
rm(regdata)


#### Regression functions ####

approval <- "approval"
voted_for_inc <- "voted_for_inc"

noctrl_func_cse <- function(x, df){
  
  if(x == "approval"){
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + 
          factor(female) * votematchmean2 + factor(dem) * factor(cong) | dist_id"))
  } else {
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + 
          factor(female) * votematchmean2 + factor(dem) * factor(cong) | dist_id2"))
  }
  feols(formula, 
        data = df,
        weights = ~weight_cumulative)
}

ctrl_func_cse <- function(x, df){
  if(x == "approval") {
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + 
          factor(female) * votematchmean2 + copartisan + majority + chair + seniority + 
          power + factor(dem) * factor(cong) | dist_id"))
  } else {
        formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + 
          factor(female) * votematchmean2 + copartisan + majority + chair + seniority + 
          power + factor(dem) * factor(cong) | dist_id2"))
  }
  feols(formula, 
        data = df,
        weights = ~weight_cumulative)
}

noctrl_func_rse <- function(x, df){
  if(x == "approval"){
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + 
          factor(female) * votematchmean2 + factor(dem) * factor(cong) | dist_id"))
  } else {
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + 
          factor(female) * votematchmean2 + factor(dem) * factor(cong) | dist_id2"))
  }
  feols(formula, 
        data = df,
        vcov_cluster(~icpsr),
        weights = ~weight_cumulative)
}

ctrl_func_rse <- function(x, df){
  if(x == "approval") {
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + 
          factor(female) * votematchmean2 + copartisan + majority + chair + seniority + 
          power + factor(dem) * factor(cong) | dist_id"))
  } else {
        formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + 
          factor(female) * votematchmean2 + copartisan + majority + chair + seniority + 
          power + factor(dem) * factor(cong) | dist_id2"))
  }
  feols(formula, 
        data = df,
        vcov_cluster(~icpsr),
        weights = ~weight_cumulative)
}

```


```{r echo = FALSE, message = FALSE, warning=FALSE}

#####################################
######### Approval (Panel A) ########
#####################################

#### Approval with no controls ####

approvalmod1rse <- noctrl_func_rse(approval, data)
approvalmod1cse <- noctrl_func_cse(approval, data)

#### Approval with all controls ####

approvalmod2rse <- ctrl_func_rse(approval, data)
approvalmod2cse <- ctrl_func_cse(approval, data)


#######################################
######## Vote choice (Panel B) ########
#######################################

### Vote choice with no controls ###

vote_choicemod1rse <- noctrl_func_rse(voted_for_inc, data)
vote_choicemod1cse <- noctrl_func_cse(voted_for_inc, data)

### Vote choice with controls ###

vote_choicemod2rse <- ctrl_func_rse(voted_for_inc, data)
vote_choicemod2cse <- ctrl_func_cse(voted_for_inc, data)


###################################
##### Extract standard errors #####
###################################


approvalmod1ses <- data.frame("Estimate" = summary(approvalmod1cse)$coeftable[,1],
                 "ClassicalSE" = summary(approvalmod1cse)$coeftable[,2],
                 "RobustSE" = as.array(approvalmod1rse$coeftable[,2]))
approvalmod1ses %>% 
  kable(digits = 4, booktabs = T) %>% 
  kable_styling(latex_options = "striped") %>% 
  add_header_above("Classical and Robust Standard Errors, Approval, Model 1")


approvalmod2ses <- data.frame("Estimate" = summary(approvalmod2cse)$coeftable[,1],
                 "ClassicalSE" = summary(approvalmod2cse)$coeftable[,2],
                 "RobustSE" = as.array(approvalmod2rse$coeftable[,2]))
approvalmod2ses %>% 
  kable(digits = 4, booktabs = T) %>% 
  kable_styling(latex_options = "striped")%>% 
  add_header_above("Classical and Robust Standard Errors, Approval, Model 2")

vote_choicemod1ses <- data.frame("Estimate" = summary(vote_choicemod1cse)$coeftable[,1],
                 "ClassicalSE" = summary(vote_choicemod1cse)$coeftable[,2],
                 "RobustSE" = as.array(vote_choicemod1rse$coeftable[,2]))
vote_choicemod1ses %>% 
  kable(digits = 4, booktabs = T) %>% 
  kable_styling(latex_options = "striped") %>% 
  add_header_above("Classical and Robust Standard Errors, Vote, Model 1")

vote_choicemod2ses <- data.frame("Estimate" = summary(vote_choicemod2cse)$coeftable[,1],
                 "ClassicalSE" = summary(vote_choicemod2cse)$coeftable[,2],
                 "RobustSE" = as.array(vote_choicemod2rse$coeftable[,2]))
vote_choicemod2ses %>% 
  kable(digits = 4, booktabs = T) %>% 
  kable_styling(latex_options = "striped") %>% 
  add_header_above("Classical and Robust Standard Errors, Vote, Model 2")

```


```{r echo = FALSE, message = FALSE, warning=FALSE}

#####################################
######### Approval (Panel A) ########
#####################################

### Approval with no controls ###

approvalmod1 <- approvalmod1cse

approvalmod1log <- feglm(approval ~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + factor(female) * votematchmean2 + factor(dem) * factor(cong) 
                     | dist_id, 
                     data = data,
                     family = binomial(link = "logit"),
                     weights = ~weight_cumulative)

approvalmod1prob <- feglm(approval ~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + factor(female) * votematchmean2 + factor(dem) * factor(cong) 
                     | dist_id, 
                     data = data,
                     family = binomial(link = "probit"),
                     weights = ~weight_cumulative)

### Approval with all controls ###

approvalmod2 <- approvalmod2rse

approvalmod2log <- feglm(approval ~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + factor(female) * votematchmean2 + copartisan + majority + chair + seniority + power + factor(dem) * factor(cong)
                      | dist_id, 
                      data = data, 
                      family = binomial(link = "logit"),
                      weights = ~weight_cumulative)

approvalmod2prob <- feglm(approval ~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + factor(female) * votematchmean2 + copartisan + majority + chair + seniority + power + factor(dem) * factor(cong)
                      | dist_id, 
                      data = data, 
                      family = binomial(link = "probit"),
                      weights = ~weight_cumulative)


#######################################
######## Vote choice (Panel B) ########
#######################################

### Vote choice with no controls ###

vote_choicemod1 <- vote_choicemod1cse

vote_choicemod1log <- feglm(voted_for_inc ~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + factor(female) * votematchmean2 + factor(dem) * factor(cong) 
                      | dist_id2, 
                      data = data, 
                      family = binomial(link = "logit"),
                      weights = ~weight_cumulative)

vote_choicemod1prob <- feglm(voted_for_inc ~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + factor(female) * votematchmean2 + factor(dem) * factor(cong) 
                      | dist_id2, 
                      data = data, 
                      family = binomial(link = "probit"),
                      weights = ~weight_cumulative)


### Vote choice with controls ###

vote_choicemod2 <- vote_choicemod2cse

vote_choicemod2log <- feglm(voted_for_inc ~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + factor(female) * votematchmean2 + copartisan + majority + chair + seniority + power + factor(dem) * factor(cong) 
                         | dist_id2, 
                         data = data, 
                         family = binomial(link = "logit"),
                         weights = ~weight_cumulative)

vote_choicemod2prob <- feglm(voted_for_inc ~ factor(female) + votematchmean2 + factor(dem) + factor(cong) + factor(female) * votematchmean2 + copartisan + majority + chair + seniority + power + factor(dem) * factor(cong) 
                         | dist_id2, 
                         data = data, 
                         family = binomial(link = "probit"),
                         weights = ~weight_cumulative)


####################################
##### Create models for tables #####
####################################

models_approval <- list("OLS 1" = approvalmod1, "Logit 1" = approvalmod1log, "Probit 1" = approvalmod1prob, "OLS 2" = approvalmod2, "Logit 2" = approvalmod2log, "Probit 2" = approvalmod2prob)

models_vote <- list("OLS 1" = vote_choicemod1, "Logit 1" = vote_choicemod1log, "Probit 1" = vote_choicemod1prob, "OLS 2" = vote_choicemod2, "Logit 2" = vote_choicemod2log, "Probit 2" = vote_choicemod2prob)

## Row names for tables

row_names <- c("factor(female)1" = "Member is a Woman", "votematchmean2" = "Policy Disagreement", "factor(dem)1" = "Member is a Democrat", "factor(cong)111" = "111th Congress", "factor(cong)112" = "112th Congress", "factor(cong)113" = "113th Congress", "factor(cong)114" = "114th Congress", "factor(cong)115" = "115th Congress")

## Table 1

row_order <- c("factor(female)1" = "Member is a Woman", "votematchmean2" = "Policy Disagreement", "factor(female)1:votematchmean2" = "Member is a Woman + Policy Disagreement")


rows <- tribble(~term,          ~OLS1,  ~Logit1, ~Probit1, ~OLS2, ~Logit2, ~Probit2,
                'District fixed-effects', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
                'Party-year fixed-effects', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
                'Controls', 'No',  'No', 'No', 'Yes', 'Yes', 'Yes')
attr(rows, 'position') <- c(7, 8, 9)

Table1 <- modelsummary(models_approval, 
             coef_map = row_order, 
             stars = c('*' = .1, '**' = .05),
             gof_map = c("nobs"),
             title = 'The Relationship between Legislative Behavior and Constituent Approval \n(OLS vs. Logit vs. Probit)',
             add_rows = rows,
             output = "kableExtra",
             threeparttable = TRUE,
             notes = list("Entries are coefficients based on three different models: Ordinary Least Squares (OLS), Logit, and Probit. Data are from the 2008, 2010, 2012, 2014, 2016, and 2018 CCES. The first set of models includes no controls, while the second set includes controls for copartisan status with the respondent, the legislator’s seniority, and whether the legislator is a member of the majority party, a committee chair, or serves on a powerful committee."))

Table1 %>% 
  kable_styling(latex_options = c("scale_down", "striped"))

## Table 2

row_order <- c("factor(female)1" = "Member is a Woman", "votematchmean2" = "Policy Disagreement", "factor(female)1:votematchmean2" = "Member is a Woman + Policy Disagreement")

rows <- tribble(~term,          ~OLS1,  ~Logit1, ~Probit1, ~OLS2, ~Logit2, ~Probit2,
                'District fixed-effects', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
                'Party-year fixed-effects', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes',
                'Controls', 'No',  'No', 'No', 'Yes', 'Yes', 'Yes')
attr(rows, 'position') <- c(7, 8, 9)

Table2 <- modelsummary(models_vote, 
             coef_map = row_order, 
             stars = c('*' = .1, '**' = .05),
             gof_map = c("nobs"),
             title = 'The Relationship between Legislative Behavior and Constituent Vote  \n(OLS vs. Logit vs. Probit)',
             add_rows = rows,
             output = "kableExtra",
             threeparttable = TRUE,
             notes = list("Entries are coefficients based on three different models: Ordinary Least Squares (OLS), Logit, and Probit. Data are from the 2008, 2010, 2012, 2014, 2016, and 2018 CCES. The first set of models includes no controls, while the second set includes controls for copartisan status with the respondent, the legislator’s seniority, and whether the legislator is a member of the majority party, a committee chair, or serves on a powerful committee."))

Table2 %>% 
  kable_styling(latex_options = c("scale_down", "striped"))

```


```{r echo = FALSE, message=FALSE, warning=FALSE}


#### Create filtered datasets ####
filter(data, cong == 110) -> cong110data
filter(data, cong == 111) -> cong111data
filter(data, cong == 112) -> cong112data
filter(data, cong == 113) -> cong113data
filter(data, cong == 114) -> cong114data
filter(data, cong == 115) -> cong115data

#### Functions for individual congresses ####

cong_noctrl_func_cse <- function(x, df){
  
  if(x == "approval"){
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(female) * votematchmean2 | dist_id"))
  } else {
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + factor(female) * votematchmean2 | dist_id2"))
  }
  feols(formula, 
        data = df,
        weights = ~weight_cumulative)
}

cong_ctrl_func_cse <- function(x, df){
  if(x == "approval") {
    formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + 
          factor(female) * votematchmean2 + copartisan + majority + chair + seniority + 
          power | dist_id"))
  } else {
        formula <- as.formula(paste(x, "~ factor(female) + votematchmean2 + factor(dem) + 
          factor(female) * votematchmean2 + copartisan + majority + chair + seniority + 
          power | dist_id2"))
  }
  feols(formula, 
        data = df,
        weights = ~weight_cumulative)
}


#####################################
######### Approval (Panel A) ########
#####################################

#### Congress Approval no controls ####

cong_noctrl_func_cse(approval, cong110data) -> cong110approvalmod1
cong_noctrl_func_cse(approval, cong111data) -> cong111approvalmod1
cong_noctrl_func_cse(approval, cong112data) -> cong112approvalmod1
cong_noctrl_func_cse(approval, cong113data) -> cong113approvalmod1
cong_noctrl_func_cse(approval, cong114data) -> cong114approvalmod1
cong_noctrl_func_cse(approval, cong115data) -> cong115approvalmod1

congapprovalmods1 <- data.frame("110th Congress Estimate" = as.array(cong110approvalmod1)$coeftable[,1],
                               "111th Congress Estimate" = as.array(cong111approvalmod1)$coeftable[,1],
                               "112th Congress Estimate" = as.array(cong112approvalmod1)$coeftable[,1],
                               "113th Congress Estimate" = as.array(cong113approvalmod1)$coeftable[,1],
                               "114th Congress Estimate" = as.array(cong114approvalmod1)$coeftable[,1],
                               "115th Congress Estimate" = as.array(cong115approvalmod1)$coeftable[,1],
                               "110th Congress SE" = as.array(cong110approvalmod1)$coeftable[,2],
                               "111th Congress SE" = as.array(cong111approvalmod1)$coeftable[,2],
                               "112th Congress SE" = as.array(cong112approvalmod1)$coeftable[,2],
                               "113th Congress SE" = as.array(cong113approvalmod1)$coeftable[,2],
                               "114th Congress SE" = as.array(cong114approvalmod1)$coeftable[,2],
                               "115th Congress SE" = as.array(cong115approvalmod1)$coeftable[,2])


#### Congress Approval with controls ####

cong_ctrl_func_cse(approval, cong110data) -> cong110approvalmod2
cong_ctrl_func_cse(approval, cong111data) -> cong111approvalmod2
cong_ctrl_func_cse(approval, cong112data) -> cong112approvalmod2
cong_ctrl_func_cse(approval, cong113data) -> cong113approvalmod2
cong_ctrl_func_cse(approval, cong114data) -> cong114approvalmod2
cong_ctrl_func_cse(approval, cong115data) -> cong115approvalmod2

congapprovalmods2 <- data.frame("110th Congress Estimate" = as.array(cong110approvalmod2)$coeftable[,1],
                               "111th Congress Estimate" = as.array(cong111approvalmod2)$coeftable[,1],
                               "112th Congress Estimate" = as.array(cong112approvalmod2)$coeftable[,1],
                               "113th Congress Estimate" = as.array(cong113approvalmod2)$coeftable[,1],
                               "114th Congress Estimate" = as.array(cong114approvalmod2)$coeftable[,1],
                               "115th Congress Estimate" = as.array(cong115approvalmod2)$coeftable[,1],
                               "110th Congress SE" = as.array(cong110approvalmod2)$coeftable[,2],
                               "111th Congress SE" = as.array(cong111approvalmod2)$coeftable[,2],
                               "112th Congress SE" = as.array(cong112approvalmod2)$coeftable[,2],
                               "113th Congress SE" = as.array(cong113approvalmod2)$coeftable[,2],
                               "114th Congress SE" = as.array(cong114approvalmod2)$coeftable[,2],
                               "115th Congress SE" = as.array(cong115approvalmod2)$coeftable[,2])


#######################################
######## Vote choice (Panel B) ########
#######################################

#### Congress Vote Choice no controls ####

cong_noctrl_func_cse(voted_for_inc, cong110data) -> cong110_voted_for_inc_mod1
cong_noctrl_func_cse(voted_for_inc, cong111data) -> cong111_voted_for_inc_mod1
cong_noctrl_func_cse(voted_for_inc, cong112data) -> cong112_voted_for_inc_mod1
cong_noctrl_func_cse(voted_for_inc, cong113data) -> cong113_voted_for_inc_mod1
cong_noctrl_func_cse(voted_for_inc, cong114data) -> cong114_voted_for_inc_mod1
cong_noctrl_func_cse(voted_for_inc, cong115data) -> cong115_voted_for_inc_mod1

cong_voted_for_inc_mods1 <- data.frame("110th Congress Estimate" = as.array(cong110_voted_for_inc_mod1)$coeftable[,1],
                               "111th Congress Estimate" = as.array(cong111_voted_for_inc_mod1)$coeftable[,1],
                               "112th Congress Estimate" = as.array(cong112_voted_for_inc_mod1)$coeftable[,1],
                               "113th Congress Estimate" = as.array(cong113_voted_for_inc_mod1)$coeftable[,1],
                               "114th Congress Estimate" = as.array(cong114_voted_for_inc_mod1)$coeftable[,1],
                               "115th Congress Estimate" = as.array(cong115_voted_for_inc_mod1)$coeftable[,1],
                               "110th Congress SE" = as.array(cong110_voted_for_inc_mod1)$coeftable[,2],
                               "111th Congress SE" = as.array(cong111_voted_for_inc_mod1)$coeftable[,2],
                               "112th Congress SE" = as.array(cong112_voted_for_inc_mod1)$coeftable[,2],
                               "113th Congress SE" = as.array(cong113_voted_for_inc_mod1)$coeftable[,2],
                               "114th Congress SE" = as.array(cong114_voted_for_inc_mod1)$coeftable[,2],
                               "115th Congress SE" = as.array(cong115_voted_for_inc_mod1)$coeftable[,2])


#### Congress Vote Choice with controls ####

cong_ctrl_func_cse(voted_for_inc, cong110data) -> cong110_voted_for_inc_mod2
cong_ctrl_func_cse(voted_for_inc, cong111data) -> cong111_voted_for_inc_mod2
cong_ctrl_func_cse(voted_for_inc, cong112data) -> cong112_voted_for_inc_mod2
cong_ctrl_func_cse(voted_for_inc, cong113data) -> cong113_voted_for_inc_mod2
cong_ctrl_func_cse(voted_for_inc, cong114data) -> cong114_voted_for_inc_mod2
cong_ctrl_func_cse(voted_for_inc, cong115data) -> cong115_voted_for_inc_mod2

cong_voted_for_inc_mods2 <- data.frame("110th Congress Estimate" = as.array(cong110_voted_for_inc_mod2)$coeftable[,1],
                               "111th Congress Estimate" = as.array(cong111_voted_for_inc_mod2)$coeftable[,1],
                               "112th Congress Estimate" = as.array(cong112_voted_for_inc_mod2)$coeftable[,1],
                               "113th Congress Estimate" = as.array(cong113_voted_for_inc_mod2)$coeftable[,1],
                               "114th Congress Estimate" = as.array(cong114_voted_for_inc_mod2)$coeftable[,1],
                               "115th Congress Estimate" = as.array(cong115_voted_for_inc_mod2)$coeftable[,1],
                               "110th Congress SE" = as.array(cong110_voted_for_inc_mod2)$coeftable[,2],
                               "111th Congress SE" = as.array(cong111_voted_for_inc_mod2)$coeftable[,2],
                               "112th Congress SE" = as.array(cong112_voted_for_inc_mod2)$coeftable[,2],
                               "113th Congress SE" = as.array(cong113_voted_for_inc_mod2)$coeftable[,2],
                               "114th Congress SE" = as.array(cong114_voted_for_inc_mod2)$coeftable[,2],
                               "115th Congress SE" = as.array(cong115_voted_for_inc_mod2)$coeftable[,2])


####################################
##### Create models for tables #####
####################################

app_congress_mod1 <- list("110th Congress" = cong110approvalmod1, "111th Congress" = cong111approvalmod1, "112th Congress" = cong112approvalmod1, "113th Congress" = cong113approvalmod1, "114th Congress" = cong114approvalmod1, "115th Congress" = cong115approvalmod1)

app_congress_mod2 <- list("110th Congress" = cong110approvalmod2, "111th Congress" = cong111approvalmod2, "112th Congress" = cong112approvalmod2, "113th Congress" = cong113approvalmod2, "114th Congress" = cong114approvalmod2, "115th Congress" = cong115approvalmod2)

vote_congress_mod1 <- list("110th Congress" = cong110_voted_for_inc_mod1,"111th Congress" = cong111_voted_for_inc_mod1, "112th Congress" = cong112_voted_for_inc_mod1, "113th Congress" = cong113_voted_for_inc_mod1, "114th Congress" = cong114_voted_for_inc_mod1, "115th Congress" = cong115_voted_for_inc_mod1)

vote_congress_mod2 <- list("110th Congress" = cong110_voted_for_inc_mod2,"111th Congress" = cong111_voted_for_inc_mod2, "112th Congress" = cong112_voted_for_inc_mod2, "113th Congress" = cong113_voted_for_inc_mod2, "114th Congress" = cong114_voted_for_inc_mod2, "115th Congress" = cong115_voted_for_inc_mod2)



## Table 3: Approval by Congress with no controls


Table3 <- modelsummary(app_congress_mod1, 
             coef_map = row_order, 
             stars = c('*' = .1, '**' = .05),
             gof_map = c("nobs"),
             threeparttable = TRUE,
             title = 'Relationship between Legislative Behavior and Constituent Approval for each Congress (Model 1 - No Controls)',
             output = "kableExtra",
             notes = c("Entries are linear regression coefficients with standard errors in parentheses, based on data from 2008, 2010, 2012, 2014, 2016, and 2018 CCES. This model does not include controls."))

Table3 %>% 
  kable_styling(latex_options = c("scale_down", "striped")) %>% 
  column_spec(2:7, width = "2cm") %>% 
  column_spec(1, width = "4cm")
  


## Table 4: Approval by Congress with controls

Table4 <- modelsummary(app_congress_mod2, 
             coef_map = row_order, 
             stars = c('*' = .1, '**' = .05),
             gof_map = c("nobs"),
             title = 'Relationship between Legislative Behavior and Constituent Approval for each  Congress (Model 2 - Including Controls)',
             output = "kableExtra",
             threeparttable = TRUE,
             notes = list("Entries are linear regression coefficients with standard errors in parentheses, based on data from 2008, 2010, 2012, 2014, 2016, and 2018 CCES. This model does includes controls for for copartisan status with the respondent, the legislator’s seniority, and whether the legislator is a member of the majority party, a committee chair, or serves on a powerful committee.")) 

Table4 %>% 
  kable_styling(latex_options = c("scale_down", "striped")) %>% 
  column_spec(2:7, width = "2cm") %>% 
  column_spec(1, width = "4cm")


## Table 5: Vote by Congress, no controls

Table5 <- modelsummary(vote_congress_mod1, 
             coef_map = row_order, 
             stars = c('*' = .1, '**' = .05),
             gof_map = c("nobs"),
             title = 'Relationship between Legislative Behavior and Constituent Vote for each  Congress (Model 1 - No Controls)',
             output = "kableExtra",
             threeparttable = TRUE,
             notes = list("Entries are linear regression coefficients with standard errors in parentheses, based on data from 2008, 2010, 2012, 2014, 2016, and 2018 CCES. This model does not include controls.")) 

Table5 %>% 
  kable_styling(latex_options = c("scale_down", "striped")) %>% 
  column_spec(2:7, width = "2cm") %>% 
  column_spec(1, width = "4cm")

## Table 6: Vote by Congress with controls

Table6 <- modelsummary(vote_congress_mod2, 
             coef_map = row_order, 
             stars = c('*' = .1, '**' = .05),
             gof_map = c("nobs"),
             title = 'Relationship between Legislative Behavior and Constituent Vote for each  Congress (Model 2 - Including Controls)',
             output = "kableExtra",
             threeparttable = TRUE,
             notes = list("Entries are linear regression coefficients with standard errors in parentheses, based on data from 2008, 2010, 2012, 2014, 2016, and 2018 CCES. This model does includes controls for for copartisan status with the respondent, the legislator’s seniority, and whether the legislator is a member of the majority party, a committee chair, or serves on a powerful committee.")) 

Table6 %>% 
  kable_styling(latex_options = c("scale_down", "striped")) %>% 
  column_spec(2:7, width = "2cm") %>% 
  column_spec(1, width = "4cm")

```


```{r echo = FALSE, message=FALSE, warning=FALSE}


#### Create filtered datasets ####
filter(data, cong <= 113) -> pre2016data
filter(data, cong == 115) -> post2016data


#####################################
######### Approval (Panel A) ########
#####################################

#### Pre- vs. Post-2016 Approval no controls ####

cong_noctrl_func_cse(approval, pre2016data) -> pre2016approvalmod1
cong_noctrl_func_cse(approval, post2016data) -> post2016approvalmod1

pre_vs_post_2016_approvalmods1 <- data.frame("Pre-2016 Estimate" = as.array(pre2016approvalmod1)$coeftable[,1],
                               "Post-2016  Estimate" = as.array(post2016approvalmod1)$coeftable[,1],
                               "Pre-2016 SE" = as.array(pre2016approvalmod1)$coeftable[,2],
                               "Post-2016 SE" = as.array(post2016approvalmod1)$coeftable[,2])

#### Pre- vs. Post-2016 Approval with controls ####

cong_ctrl_func_cse(approval, pre2016data) -> pre2016approvalmod2
cong_ctrl_func_cse(approval, post2016data) -> post2016approvalmod2

pre_vs_post_2016_approvalmods2 <- data.frame("Pre-2016 Estimate" = as.array(pre2016approvalmod2)$coeftable[,1],
                               "Post-2016  Estimate" = as.array(post2016approvalmod2)$coeftable[,1],
                               "Pre-2016 SE" = as.array(pre2016approvalmod2)$coeftable[,2],
                               "Post-2016 SE" = as.array(post2016approvalmod2)$coeftable[,2])


#######################################
######## Vote choice (Panel B) ########
#######################################

#### Congress Vote Choice no controls ####

cong_noctrl_func_cse(voted_for_inc, pre2016data) -> pre2016_voted_for_inc_mod1
cong_noctrl_func_cse(voted_for_inc, post2016data) -> post2016_voted_for_inc_mod1

pre_vs_post_voted_for_inc_mods1 <- data.frame("Pre-2016 Estimate" = as.array(pre2016_voted_for_inc_mod1)$coeftable[,1],
                               "Post-2016 Estimate" = as.array(post2016_voted_for_inc_mod1)$coeftable[,1],
                               "Pre-2016 SE" = as.array(pre2016_voted_for_inc_mod1)$coeftable[,2],
                               "Post-2016 SE" = as.array(post2016_voted_for_inc_mod1)$coeftable[,2])

#### Congress Vote Choice with controls ####

cong_ctrl_func_cse(voted_for_inc, pre2016data) -> pre2016_voted_for_inc_mod2
cong_ctrl_func_cse(voted_for_inc, post2016data) -> post2016_voted_for_inc_mod2

pre_vs_post_voted_for_inc_mods2 <- data.frame("Pre-2016 Estimate" = as.array(pre2016_voted_for_inc_mod2)$coeftable[,1],
                               "Post-2016 Estimate" = as.array(post2016_voted_for_inc_mod2)$coeftable[,1],
                               "Pre-2016 SE" = as.array(pre2016_voted_for_inc_mod2)$coeftable[,2],
                               "Post-2016 SE" = as.array(post2016_voted_for_inc_mod2)$coeftable[,2])

####################################
##### Create models for tables #####
####################################

app_2016 <- list("Pre-2016 (Model 1)" = pre2016approvalmod1, "Post-2016 (Model 1)" = post2016approvalmod1, "Pre-2016 (Model 2)" = pre2016approvalmod2, "Post-2016 (Model 2)" = post2016approvalmod2)

vote_2016 <- list("Pre-2016 (Model 1)" = pre2016_voted_for_inc_mod1, "Post-2016 (Model 1)" = post2016_voted_for_inc_mod1, "Pre-2016 (Model 2)" = pre2016_voted_for_inc_mod2, "Post-2016 (Model 2)" = post2016_voted_for_inc_mod2)


## Table 7: Approval Pre- and Post-2016 with No Controls


Table7 <- modelsummary(app_2016, 
             coef_map = row_order, 
             stars = c('*' = .1, '**' = .05),
             gof_map = c("nobs"),
             threeparttable = TRUE,
             title = 'Relationship between Legislative Behavior and Constituent Approval, 2008-2014 vs. 2016-2018',
             output = "kableExtra",
             notes = c("Entries are linear regression coefficients with standard errors in parentheses, based on data from 2008, 2010, 2012, 2014, and 2018 CCES. The first set of models includes no controls, while the second set includes controls for copartisan status with the respondent, the legislator’s seniority, and whether the legislator is a member of the majority party, a committee chair, or serves on a powerful committee."))

Table7 %>% 
  kable_styling(latex_options = c("scale_down", "striped")) %>% 
  column_spec(2:7, width = "2cm") %>% 
  column_spec(1, width = "4cm")


## Table 8: Vote Pre- and Post-2016 with Controls

Table8 <- modelsummary(vote_2016, 
             coef_map = row_order, 
             stars = c('*' = .1, '**' = .05),
             gof_map = c("nobs"),
             threeparttable = TRUE,
             title = 'Relationship between Legislative Behavior and Constituent Vote, 2008-2014 vs. 2016-2018',
             output = "kableExtra",
             notes = c("Entries are linear regression coefficients with standard errors in parentheses, based on data from 2008, 2010, 2012, 2014, and 2018 CCES. The first set of models includes no controls, while the second set includes controls for copartisan status with the respondent, the legislator’s seniority, and whether the legislator is a member of the majority party, a committee chair, or serves on a powerful committee."))

Table8 %>% 
  kable_styling(latex_options = c("scale_down", "striped")) %>% 
  column_spec(2:7, width = "2cm") %>% 
  column_spec(1, width = "4cm")

```
